<div class="main layui-clear">
    <h2 class="page-title">
        发表问题
    </h2>
    <div class="layui-form layui-form-pane">
        <form method="post" action="">
            <div class="layui-form-item">
                <label class="layui-form-label" for="L_title">
                    标题
                </label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" autocomplete="off" lay-verify="title"
                    required="" name="title" id="L_title">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <div class="layui-input-block">
                    <textarea style="height: 260px;" class="layui-textarea fly-editor" placeholder="请输入内容"
                    lay-verify="" required="" name="content" id="L_content"></textarea>
                </div>
                <label style="top: -2px;" class="layui-form-label" for="L_content">
                    描述
                </label>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">
                        所在类别
                    </label>
                    <div class="layui-input-block">
                        <select lay-verify="required" name="cag_id">
                            <option></option>
                            <?php foreach ($cateData as $v) { ?>
                                <optgroup label="<?php echo $v['category'] ?>">
                                    <?php foreach ($v['son'] as $value) { ?>
                                        <option value="<?php echo $value['cag_id'] ?>"><?php echo $value['category'] ?></option>
                                    <?php } ?>
                                </optgroup>
                            <?php } ?>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">
                        悬赏飞吻
                    </label>
                    <div class="layui-input-block">
                        <select name="kiss">
                            <option value="5">
                                5
                            </option>
                            <option value="20">
                                20
                            </option>
                            <option value="50">
                                50
                            </option>
                            <option value="100">
                                100
                            </option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="L_vercode">
                    人类验证
                </label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" autocomplete="off" placeholder="请回答后面的问题"
                    lay-verify="required" required="" name="vercode" id="L_vercode">
                </div>
                <div class="layui-form-mid">
                    <span style="color: #c00;">
                        {$question}
                    </span>
                </div>
            </div>
            <div class="layui-form-item">
                <button lay-submit="" lay-filter="qsubmit" class="layui-btn">
                    立即发布
                </button>
            </div>
        </form>
    </div>
</div>
<script>
layui.use(['form', 'layedit'],function() {
    var form = layui.form(),
    layer = layui.layer,
    layedit = layui.layedit;

    layedit.set({
        uploadImage: {
            url: "{:U('home/question/qimgupload')}" //接口url
            ,
            type: 'post' //默认post
        }
    });

    //创建一个编辑器
    editIndex = layedit.build('L_content', {
        tool: ['face', 'image', 'link', 'code', 'yulan']
    });

    //自定义验证规则
    form.verify({
        title: function(obj) {
            if (obj.length < 5) {
                return '标题至少得5个字符啊';
            }
        }
    });
    //监听提交
    form.on('submit(qsubmit)',function(data) {
        data.field.content = layedit.getContent(editIndex);
        if (data.field.content.length < 10) {
            layer.msg("内容必须大于10个字符", {
                icon: 5
            });
        } else {
            $.ajax({
                url: "{:U('home/question/qpost')}",
                type: 'POST',
                dataType: 'json',
                data: data.field,
            }).done(function(response) {
                if (response.error) {
                    layer.msg(response.error, {
                        icon: 5
                    });
                } else if (response.info) {
                    layer.msg(response.info, {
                        icon: 6
                    },
                    function() {
                        location.href = response.url;
                    });
                }
            }).fail(function() {
                layer.msg("数据传输失败", {
                    icon: 5
                });
            })
        }
        return false;
    });
});
</script>